package edu.lhh;

import java.io.IOException;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.lhh.dao.newstypedao;
import edu.lhh.dao.scoretypedao;
import edu.lhh.model.newstype;
import edu.lhh.model.scoretype;

@WebServlet("/scoreServlet")
    public class scoreServlet extends HttpServlet {
	private newstypedao dao = new newstypedao();
	private scoretypedao sdao = new scoretypedao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opt = req.getParameter("opt");
		if(!"score".equals(opt) ){
		scoretype tp = new scoretype();
		tp.setIndex(Integer.parseInt(req.getParameter("getindex").toString()));
		tp.setId(req.getParameter("sid"));
		tp.setMath(Integer.parseInt(req.getParameter("math").toString()));
		tp.setEnglish(Integer.parseInt(req.getParameter("eng").toString()));
		tp.setPe(Integer.parseInt(req.getParameter("pe").toString()));
		req.setAttribute("score", tp);
		sdao.add(tp);
		}
		req.getRequestDispatcher("StudenScore.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

		List<newstype> lst;
		try {
			lst = dao.findbyidORname(name);
			newstype tp= lst.get(0);
			if(tp.getAge() == null){			 
				req.getRequestDispatcher("null.jsp").forward(req, resp);
			}
			else if ("f".equals(tp.getStf())){
				String sname = tp.getName();
				String id = tp.getId();
				int index = tp.getIndex();
				req.setAttribute("sid", id);
				req.setAttribute("sname", sname);
				req.setAttribute("index", index);
				req.getRequestDispatcher("firstScore.jsp").forward(req, resp);
				dao.changet(index);
			}else{
				lst = dao.findbyidORname(name);
				newstype tp1= lst.get(0);
				String id1 = tp1.getId();
				scoretype tp2 = sdao.findbyid(id1);
				//req.setAttribute("rs", lst);
				req.setAttribute("rs", tp2);
				req.getRequestDispatcher("result.jsp").forward(req, resp);
			}
			//System.out.println(tp.getStf());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//req.getRequestDispatcher("news_type.jsp").forward(req, resp);
	}

}
